package com.proxy.pattern.interceptor;

import com.proxy.pattern.service.UserService;
import com.proxy.pattern.service.impl.UserServiceImpl3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author cc
 * @classname UserMethodInterceptor
 * @description 定义代理类的增强逻辑
 * @date 2022/3/27 14:24
 */
@Slf4j
public class UserMethodInterceptor implements MethodInterceptor {

    /**
     * @param o     不是目标类，UserServiceImpl3$$EnhancerByCGLIB$$585e44ac
     * @param method
     * @param args
     * @param methodProxy
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        log.info("增强逻辑");
        Object obj = methodProxy.invokeSuper(o, args);
        //methodProxy.invoke(new UserServiceImpl3(), args);     该方式也可以
        log.info("增强逻辑");
        return obj;
    }

}
